import {Component, OnInit, Input, Output, EventEmitter} from '@angular/core';
import {FormGroup, FormBuilder} from "@angular/forms";
import {UserModel} from "../../model/user.model";
import {TitleCpt} from "../../lib/title/title.component";
import {LocalStorage} from "../../app.local-storage";
import {CONSTANT} from "../../service/constant.service";
import {MeterService} from "../../service/meter.service";
import {ROUTE} from "../../service/route.service";
import {CheckStandModel} from "../../model/checkstand.model";
import {USER} from "../../service/user.service";

@Component({
  selector: 'app-lifepayment-modal-view',
  templateUrl: './lifepayment-modal-view.component.html',
  styleUrls: ['./lifepayment-modal-view.component.scss']
})
export class LifepaymentModalViewComponent implements OnInit {

  @Input('showModal') public isModalShown: boolean = false;
  @Output('modalClose') public modalClose = new EventEmitter();
  moneyItems = [
    {name: '50元', value: 50},
    {name: '100元', value: 100},
    {name: '200元', value: 200}
  ];
  data = {
    contractId: undefined,
    money: 50,
    type: 2
  };
  hello: string;
  user: any = UserModel;
  paymentViewTitleCpt: Array<TitleCpt> = this.constant.genPaymentTitleCptList();

  constructor(private ls: LocalStorage,
              public METER:MeterService,
              private route:ROUTE,
              private USER:USER,
              private constant: CONSTANT) {

  }

  public showModal(): void {
    this.isModalShown = true;
  }

  public onHidden(): void {
    this.isModalShown = false;
  }

  billTypeItems: any = [
    {id:2, name: '水费充值', isActive: true},
    {id:3, name: '电费充值', isActive: false}
  ];

  toggleType(item: any,) {
    for (let i = 0; i < this.billTypeItems.length; i++) {
      this.billTypeItems[i].isActive = false;
      item.isActive = true;
    }
  }

  ngOnInit() {
    if (this.ls.getObject('user')) {
      this.user = this.ls.getObject('user') as UserModel;
    }
    this.hello = this.constant.getNewDate();
    this.USER.refreshContract.subscribe(v=> {
      console.log("USER.refreshContract",v);
      this.data.contractId = v.id;
    })
  }
  doCharge(){
    this.METER.recharge(this.data, v=> {
      console.log(v)
      let checkstand = new CheckStandModel(v.uuid, v.content, v.money, v.id, 'personal/mylifepayment');
      this.ls.setObject('checkstand', checkstand);
      this.route.goConfirmPaymentView(v.uuid);
    })

  }

}
